//
//  OtherViewController.m
//  B
//
//  Created by 王展 on 15/11/5.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "OtherViewController.h"
#import "MusicCollectionViewCell.h"
#import "Music.h"
#import "MusicDetailViewController.h"

@interface OtherViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *musicCV;
@property(nonatomic,retain)MusicCollectionViewCell *cell;

@property(nonatomic,retain)NSMutableArray *musicArray;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,assign)BOOL isupLoading;


@end

@implementation OtherViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.myAgent appearOhterBottomBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.page = 1;
    self.isupLoading = NO;
    self.musicArray = [NSMutableArray array];

    [self createMusicView];
    [self addHeaderRefresh];
    [self addFooterRefresh];
}

-(void)leftButtonAction
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}


//下拉刷新
-(void)addHeaderRefresh
{
    self.musicCV.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getMusicData];
        self.isupLoading = NO;
        
    }];
    [self.musicCV.header beginRefreshing];
}

-(void)addFooterRefresh
{
    self.musicCV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    
        self.page = self.page + 1;
        [self getMusicData];
        self.isupLoading = YES;
        
    }];
}


//创建音乐页面
-(void)createMusicView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(172 * XWIDTH, 210 * XHIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 9 * XWIDTH, 9 * XHIGHT, 9 * XWIDTH);
    flowLayout.headerReferenceSize = CGSizeMake(375 * XWIDTH, 50 * XHIGHT);
    
    self.musicCV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, (667 * XHIGHT - 49 - 64)) collectionViewLayout:flowLayout];
    self.musicCV.delegate = self;
    self.musicCV.dataSource = self;
    self.musicCV.backgroundColor = [UIColor colorWithRed:251/255.0 green:247 /255.0 blue:236/255.0 alpha:1];
    [self.view addSubview:self.musicCV];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.musicArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.musicCV registerClass:[MusicCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    self.cell.backgroundColor = [UIColor whiteColor];
    self.cell.amusic = [self.musicArray objectAtIndex:indexPath.row];

    return self.cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
    
    MusicDetailViewController *musicDetailVC =  [MusicDetailViewController sharePlayerViewController];
    [self.myAgent hideOtherBottomBar];
    musicDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:musicDetailVC animated:NO];
    
    Music *amusic = [self.musicArray objectAtIndex:indexPath.row];
    musicDetailVC.amusic = amusic;
    musicDetailVC.allMusicArray = self.musicArray;
    musicDetailVC.indexRow = indexPath.row;
}






















//获取MUSIC数据
-(void)getMusicData
{
    NSString *url_str = [NSString stringWithFormat:@"http://echosystem.kibey.com/sound/hot?page=%ld",self.page];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:@"PHPSESSID=qtur65hr4cg4d60amesrt5ndm6" forHTTPHeaderField:@"Cookie"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
        if (self.isupLoading == NO) {
            [self.musicArray removeAllObjects];
            NSLog(@"asdasdasdasdasdasd");
        }
        
        NSMutableDictionary *bigDic = responseObject;
        NSMutableDictionary *Dic = [bigDic objectForKey:@"result"];
        NSMutableArray *bigArray = [Dic objectForKey:@"data"];
        for (NSMutableDictionary *dic in bigArray) {
            Music *amusic = [[Music alloc] init];
            [amusic setValuesForKeysWithDictionary:dic];
            [self.musicArray addObject:amusic];
        }

        [self.musicCV.header endRefreshing];
        [self.musicCV.footer endRefreshing];
        [self.musicCV reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"没有网络");

    }];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
